<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\Department;
use App\Models\Position;
use App\Models\Store;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(User::with(['store', 'department', 'position']), function (Grid $grid) {
            $grid->model()->where('type', MEMBER_TYPE_USER);
            $grid->column('id')->sortable();
            $grid->column('store.title', admin_trans('user.fields.store_id'));
            $grid->column('department.title', admin_trans('user.fields.department_id'));
            $grid->column('position.title', admin_trans('user.fields.position_id'));
            $grid->column('name');
            $grid->column('phone');
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('name');
                $filter->like('phone');
                $filter->equal('store_id')
                    ->select(Store::all()->pluck('title', 'id'))
                    ->load('department_id', 'user-department');
                $filter->equal('department_id')->select()->load('position_id', 'user-position');
                $filter->equal('position_id')->select();
            });

            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, User::with(['store', 'department', 'position']), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('phone');
            $show->field('store.title',  admin_trans('user.fields.store_id'));
            $show->field('department.title', admin_trans('user.fields.department_id'));
            $show->field('position.title', admin_trans('user.fields.position_id'));
            $show->field('created_at');
            $show->field('updated_at');
            $show->panel()->tools(function ($tools) {
                $tools->disableDelete();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            if ($form->isCreating()) {
                $form->text('erp_id')->required();
                $form->text('erp_code')->required();
            }

            $form->text('name')->required();
            $form->text('phone')->required();
//            if ($form->isCreating()) {
                $form->text('basic_salary')->required();
//            }
            $form->radio('sex', admin_trans('user.fields.sex'))
                ->options(admin_trans('user.options.sex'))
                ->default(1);
            $form->radio('is_probation_period', admin_trans('user.fields.is_probation_period'))
                ->when([1], function (Form $form) {
                    $form->date('probation_period_start_time');
                    $form->date('probation_period_end_time');
                    $form->text('probation_period_salary');
                })
                ->options(admin_trans('user.options.is_probation_period'))
                ->default(2);
            $form->radio('is_on_job', admin_trans('user.fields.is_on_job'))
                ->options(admin_trans('user.options.is_on_job'))
                ->default(1);
            $form->select('store_id')
                ->options(Store::pluck('title', 'id'))
                ->load('department_id', 'user-department')
                ->required();
            $form->select('department_id')
                ->load('position_id', 'user-position')
                ->required();
            $form->select('position_id')->required();
            $form->display('created_at');
            $form->display('updated_at');

            $form->tools(function (Form\Tools $tools) {
                $tools->disableDelete();
            });
        });
    }

    public function stores(Request $request)
    {
        $brand_id = $request->get('q');

        return Store::where('brand_id', $brand_id)
            ->get(['id', DB::raw('title as text')]);
    }

    public function department(Request $request)
    {
        $store_id = $request->get('q');

        return Department::where('store_id', $store_id)
            ->get(['id', DB::raw('title as text')]);
    }

    public function position(Request $request)
    {
        $department_id = $request->get('q');

        return Position::where('department_id', $department_id)
            ->get(['id', DB::raw('title as text')]);
    }

    public function probationPeriodStatus()
    {
        $user_all = \App\Models\User::where('is_probation_period', DEFAULT_STATUS_SUCCESS)->get();

        foreach ($user_all as $key => $value) {
            if ($value->probation_period_end_time < date('Y-m-d')) {
                \App\Models\User::where('id', $value->id)->update([
                    'is_probation_period' => DEFAULT_STATUS_FAILURE
                ]);
            }
        }

        return '执行成功-'.date('Y-m-d H:i:s');
    }
}
